home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #45 (Jun 89) / XCMD Code / HyperUtils.c (Listing 2) < prev    next >
Text File  |  1989-04-10  |  4KB  |  150 lines

  1. /****************************/
  2. /* HyperUtils.c                */
  3. /*                             */
  4. /* A collection of useful     */
  5. /* routines...                */
  6. /*                             */
  7. /****************************/
  8. #include    <MacTypes.h>
  9. #include    <OSUtil.h>
  10. #include    <MemoryMgr.h>
  11. #include    <FileMgr.h>
  12. #include    <ResourceMgr.h>
  13. #include    <StdFilePkg.h>
  14. #include     "HyperXCmd.h"
  15. #include     "HyperUtils.h"
  16.  
  17. void    CenterWindow( wptr )
  18.     WindowPtr    wptr;
  19. /***************************
  20. * Center a window in the current
  21. * screen port.  Note: Does not
  22. * attempt to work with multi-screen
  23. * systems.
  24. *
  25. * This code is inspired by a
  26. * similar routine written by Steve
  27. * Maller in MPW Pascal.  Thanks Steve.
  28. ***************************/
  29. {
  30.  short     hWindSize = wptr->portRect.right - wptr->portRect.left;
  31.  short    vWindSize = wptr->portRect.bottom - wptr->portRect.top;
  32.  short    hSize = wptr->portBits.bounds.right - wptr->portBits.bounds.left;
  33.  short    vSize = wptr->portBits.bounds.bottom - wptr->portBits.bounds.top;
  34.  
  35.  MoveWindow( wptr,     
  36.              ( hSize - hWindSize ) / 2, 
  37.              ( vSize - vWindSize + 20) / 2,
  38.             false
  39.         );
  40. }
  41.  
  42. void Concat( str1, str2 )
  43.     char    *str1;
  44.     char    *str2;
  45. /*****************************
  46. * Append string 2 to the end of
  47. * string 1.  Both strings are 
  48. * pascal-format strings.
  49. *
  50. * str1 must be large enough to hold
  51. * the new string and is assumed to 
  52. * be of Type Str255 (a pascal string)
  53. *****************************/
  54. {
  55.     short len1    = *str1;        /*** the number of chars in string 1    ***/
  56.     short len2    = *str2++;        /*** the number of chars in string 2    ***/
  57.     char  *temp;                /*** string pointer                     ***/
  58.     
  59.     *str1 += len2 + 1;            /*** add sizes together to get new size    ***/
  60.  
  61.     temp = str1 + len1 + 1;        /*** move to the end of string 1        ***/
  62.     while( len2 ){
  63.         *temp++ = *str2++;        /*** add a char to temp and move along    ***/
  64.         --len2;                    /*** until all characters are added        ***/
  65.     }
  66.  
  67. }
  68.  
  69. void    CopyPStr( pStr1, pStr2 )
  70.     char    *pStr1;
  71.     char    *pStr2;
  72. /****************************
  73. * Copy the contents of pstr1 into
  74. * pstr2.  The strings are assumed 
  75. * to be of type STR255 (length byte
  76. * precedes data 
  77. *
  78. ****************************/
  79. {    short     i;
  80.     char    *tstr;
  81.     
  82.     tstr = pStr2;
  83.     
  84.     for( i = 0; i <= *pStr1; i++ )
  85.         *tstr++ = *pStr1++;
  86. }
  87.  
  88.  
  89. short GetFileNameToOpen( typs, typCnt,theName, theWDID )
  90.     SFTypeList    typs;
  91.     short        typCnt;
  92.     char        *theName;
  93.     short        *theWDID;
  94. /*****************************
  95. * Invokes SFOpenFile to query the 
  96. * user for the name of a file to 
  97. * open. 
  98. *
  99. * In:     List of types of files to
  100. *        filter for (up to 4)
  101. *
  102. * Out:    fileName if picked in theName
  103. *        working directory in theWDID
  104. *        nil otherwise
  105. *        the file's volum ref num.
  106. *
  107. * ( Note that the space for the 
  108. * string must be allocated by the
  109. * caller).
  110. *****************************/
  111. {
  112.     Point        where;
  113.     char        prompt[1];
  114.     SFReply        reply;
  115.     GrafPort    *oldPort;
  116.     WindowPtr    dlogID;
  117.     
  118.     prompt[0]         = '\0';
  119.     
  120.     /*** Get and put up the standard file    ***/
  121.     /*** dialog.  You will only see the    file***/
  122.     /*** types that you filtered for.  If     ***/
  123.     /*** you filtered for no files, then     ***/
  124.     /*** all files will display                ***/
  125.     
  126.     GetPort( &oldPort );
  127.     dlogID = GetNewDialog( (short)getDlgID, (Ptr)NIL, (Ptr)UPFRONT );
  128.     
  129.     SetPort( dlogID );
  130.     CenterWindow( dlogID );
  131.     where.h = dlogID->portRect.left;
  132.     where.v = dlogID->portRect.top;
  133.     LocalToGlobal( &where );
  134.     
  135.     SFGetFile( where, prompt, (Ptr)NIL, typCnt, typs, (Ptr)NIL, &reply );
  136.     
  137.     DisposDialog( dlogID );
  138.     SetPort( oldPort );
  139.     
  140.     /*** If the user selected a file, let's ***/
  141.     /*** get the information about it        ***/
  142.     
  143.     if (reply.good){
  144.         *theWDID = reply.vRefNum;
  145.         PtoCstr( (char *)&reply.fName );
  146.         strcpy( theName, &reply.fName  );
  147.     }
  148.     return( reply.good );
  149. }
  150.